package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.textinput.TextInputLayout;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.customView.FilterDecimalEdittext;
import com.wwwarehouse.resource_center.customView.FilterEdittext;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;

/**
 * @author Joseph on 2017/9/19.
 *         <p/>
 *         维护属性列表输入框
 */

public class GoodsAttributeProtectListFragment extends TheParentFragment  {

    private ListView lvlist;
    private ArrayList<GoodsAttributesBean.ListBean>  mAllData ;
    private RuleListAdaper mAdapter ;
    private int pos;
    private RelativeLayout rl ;
    private LinearLayout   ll;
    private int num;
    private View inflate;

    CallBackListener callBackListener;

    public void setCallBackListener(CallBackListener callBackListener) {
        this.callBackListener = callBackListener;
    }

    public  interface CallBackListener{
        void bindEditText(EditText et);
        void setOnClickListener();
    }
    @Override
    protected int getContentId() {
        return R.layout.fragment_goods_attribute_list;
    }

    @Override
    protected void init(View view) {
        ll=findView(view,R.id.ll);
        rl=findView(view,R.id.rl);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBackListener!=null){
                    callBackListener.setOnClickListener();
                }
            }
        });
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        mAllData = b.getParcelableArrayList("data");
        pos = b.getInt("pos");
        num = b.getInt("num");
        rl.setVisibility(pos==0?View.VISIBLE:View.GONE);
        ViewTreeObserver vto = ll.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            @Override
            public void onGlobalLayout() {
                ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int mHeight = ll.getHeight()- ConvertUtils.dip2px(mActivity,70); //减去底边距
                int mItemHeight ;
                mItemHeight = mHeight / num;
                LogUtils.showInfoLog("ll.getHeight():"+ll.getHeight());
                for (int i = 0; i < mAllData.size(); i++) {
                    inflate = LayoutInflater.from(mActivity).inflate(R.layout.adapter_goods_attribute, null);
                    ll.addView(inflate);
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) inflate.getLayoutParams();
                    layoutParams.height = mItemHeight ;
                    inflate.setLayoutParams(layoutParams);
                    TextInputLayout til = (TextInputLayout) inflate.findViewById(R.id.til);
                    FilterDecimalEdittext byId = (FilterDecimalEdittext) inflate.findViewById(R.id.goods_attribute);
                    byId.setHint(mAllData.get(i).getAttributeName());
                    if (callBackListener!=null){
                        callBackListener.bindEditText(byId);
                    }
//                    if (!TextUtils.isEmpty(mAllData.get(i).getAttributeValue())){
                        byId.setText(mAllData.get(i).getAttributeValue());
//                    }
                    til.updateHint();
                }
            }
        });
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsAttributeProtectListFragment){
            mActivity.setTitle(getString(R.string.attribute_protect));
        }
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {

    }

    private class RuleListAdaper extends BaseAdapter {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHodler viewHodler;
            if (convertView == null) {
                viewHodler = new ViewHodler();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_goods_attribute, parent, false);
                ViewTreeObserver vto = lvlist.getViewTreeObserver();
                final View finalConvertView = convertView;
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
                    @Override
                    public void onGlobalLayout() {
                        lvlist.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        int mHeight = lvlist.getHeight()- ConvertUtils.dip2px(mActivity,70); //减去底边距
                        int mItemHeight ;
                         mItemHeight = mHeight / num;
                        LogUtils.showInfoLog("lvlist.getHeight():"+lvlist.getHeight());
                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        layoutParams.height = mItemHeight ;
                        finalConvertView.setLayoutParams(layoutParams);
                    }
                });
                viewHodler.mAttribute = (FilterEdittext) convertView.findViewById(R.id.goods_attribute);
                viewHodler.til = (TextInputLayout) convertView.findViewById(R.id.til);
                convertView.setTag(viewHodler);
            } else {
                viewHodler = (ViewHodler) convertView.getTag();
            }
            viewHodler.mAttribute.setHint(mAllData.get(position).getAttributeName());
            viewHodler.til.updateHint();
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            return convertView;
        }
        @Override
        public int getCount() {
            return mAllData.size();
        }

        @Override
        public Object getItem(int position) {
            return mAllData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }
    class ViewHodler{
        FilterEdittext mAttribute;
        TextInputLayout til ;
    }
}
